package cn.itcast.service;

import cn.itcast.dao.PermissionMapper;
import cn.itcast.domain.Permission;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {


    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * 新增权限
     *
     * @param permission
     */
    @Override
    public void insertPermission(Permission permission) {
        permissionMapper.insertPermission(permission);



    }

    /**
     * 分页查询 条件查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPermissionByPage(QueryPageBean queryPageBean) {

        //健壮性判断
        Integer currentPage = queryPageBean.getCurrentPage();
        if (currentPage == null) {
            currentPage = 1;
        }
        Integer pageSize = queryPageBean.getPageSize();
        if (pageSize == null) {
            pageSize = 10;
        }
        PageHelper.startPage(currentPage, pageSize);

        Page<Permission> page = (Page<Permission>) permissionMapper.findPermissionByPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 删除权限
     *
     * @param id
     */
    @Override
    public void deletePermission(Integer id) {
        //判断是否已经关联 先查询
        //先删除中间表的Permission
        permissionMapper.deleteRolePermissionId(id);
        //再删除
        permissionMapper.deletePermission(id);

    }

    /**
     * 单个查询权限
     *
     * @param id
     * @return
     */
    @Override
    public Permission findPermissionById(Integer id) {

        return permissionMapper.findPermissionById(id);
    }

    /**
     * 修改权限
     *
     * @param permission
     */
    @Override
    public void updatePermission(Permission permission) {
        permissionMapper.updatePermission(permission);
    }

    /**
     * 查询所有的权限
     * @return
     */
    @Override
    public List<Permission> findAllPermissions() {
        return permissionMapper.findAllPermissions();
    }
}
